package com.mach.platform.sprider.getter;

import com.mach.platform.sprider.Conf;
import com.mach.platform.util.SqlHelper;
import com.mach.platform.util.U;

import java.sql.SQLException;
import java.util.Collections;
import java.util.Map;

public class DataCenter {


    private static DataCenter me = null;
    public static final SqlHelper sqlHelper = Conf.getSqlHelper();
    public static final SqlHelper sqlHelperExt = Conf.getSqlHelperExt();


    private DataCenter(){

    }

    public static DataCenter get(){
        if(null == me){
            me = new DataCenter();
        }
        return me;
    }

    public void start(){
        CmdMainWork cmw = new CmdMainWork();
        U.cachedThreadPool.execute(cmw);


        MainWork mw = new MainWork();
        U.cachedThreadPool.execute(mw);

        //mw.start();
    }

    public static void main(String[] args){

        DataCenter.get().start();

    }

    public static Map<Object,Map<String,Object>> curDataJustData(Long id) {


        Map<Object,Map<String,Object>> dbData = Collections.EMPTY_MAP;
        try {
            dbData = sqlHelper.query("select data_type dataType,dev_id devId, dvalue from iot_dev_cur_data_win win where 1=1 " +
                    " and dev_id=" + id +
                    ""
            );
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return dbData;
    }

    public static Map<String,Object> curDevDataHasType(Integer id, String dataType)  {




        Map<String, Object> dbData = Collections.EMPTY_MAP;
        try {
            dbData = sqlHelper.queryOne("select data_type dataType,dev_id devId, dvalue from iot_dev_cur_data_win win where 1=1 " +
                    " and dev_id=" + id +
                    " and data_type='" + dataType + "'"
            );
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return dbData;
    }
    public static Map<Object, Object> curDevDataTypeMap(Integer id)  {



        Map<Object, Object> dbData = Collections.EMPTY_MAP;
        try {
            dbData = sqlHelper.getMapFrTable("dataType", "dvalue","select data_type dataType,dev_id devId, dvalue from iot_dev_cur_data_win win where 1=1 " +
                    " and dev_id=" + id);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return dbData;
    }


    public static Double curDevDataTypeValue(Integer id, String dataType)  {


        Double result = null;

        try {
            Map<Object, Object> map = curDevDataTypeMap(id);
            if(map != null){
                result = (Double) map.get(dataType);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }


        return result;
    }
}
